
package com.ami.kvm.jviewer.communication;

import com.ami.kvm.jviewer.Debug;
import com.ami.kvm.jviewer.JViewer;
import com.ami.kvm.jviewer.gui.JVFrame;
import com.ami.kvm.jviewer.gui.JViewerApp;
import com.ami.kvm.jviewer.gui.KVMSharing;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;
import javax.net.ssl.SSLSocket;

public abstract class Client {
	protected Socket m_sock = null;
	protected Socket m_sock_nonssl = null;
	protected SSLSocket m_sock_ssl = null;

	protected byte[] m_ipAddr;

	protected int m_port;

	public Client(byte[] paramArrayOfByte, int paramInt) {
		this.m_ipAddr = paramArrayOfByte;
		this.m_port = paramInt;
	}

	public void SetSocket(Socket paramSocket) {
		this.m_sock = paramSocket;
	}

	public Socket getSocket() {
		return this.m_sock;
	}

	protected int connectVideo(boolean paramBoolean) {
		try {
			int i = paramBoolean == true ? 2 : 0;
			this.m_sock = JViewerApp.getInstance().getConnection().createSocket(InetAddress.getByAddress(this.m_ipAddr),
					this.m_port, i);
		} catch (IOException localIOException) {
			Debug.out.println(localIOException);
			return -1;
		}

		if (this.m_sock == null) {
			if ((paramBoolean) && (JViewerApp.getInstance().getConnection().getConnErrCode() == Connection.SUCCESS)) {
				JViewerApp.getInstance().getMainWindow().windowClosed();
			} else {
				return -1;
			}
		}

		if (JViewer.isKVMReconnectEnabled()) {//true
			JViewerApp.getInstance().setLocalport(this.m_sock.getLocalPort());
		}
		SetSocket(this.m_sock);
		String str = this.m_sock.getLocalAddress().toString();

		String[] arrayOfString = str.split("/");
		KVMSharing.KVM_CLIENT_OWN_IP = arrayOfString[1];
		if (KVMSharing.KVM_CLIENT_OWN_IP.compareToIgnoreCase("0.0.0.0") == 0) {
			KVMSharing.KVM_CLIENT_OWN_IP = getipv6IP();
		}
		Debug.out.println("m_sock.socket().getLocalAddress()" + str.toString());

		Debug.out.println("m_sock.socket().getLocalAddress()" + KVMSharing.KVM_CLIENT_OWN_IP);

		return 0;
	}

	public int sendMessage(byte[] paramArrayOfByte, int paramInt) {
		if ((!JViewer.isplayerapp()) && (!JViewer.isdownloadapp())) {
			try {
				getSocket().getOutputStream().write(paramArrayOfByte);
			} catch (IOException localIOException) {
				if ((JViewer.isKVMReconnectEnabled()) && ((JViewer.isjviewerapp()) || (JViewer.isStandAloneApp()))) {
					JViewerApp.getInstance().onReconnect();
				}
				Debug.out.println("Send message failed");
				Debug.out.println(localIOException);
				return 0;
			}
		}
		if (JViewer.isKVMReconnectEnabled()) {
			JViewerApp.getInstance().setLastPckSent();
		}
		return paramInt;
	}

	public void VideoSocketclose() {
		try {
			if (JViewer.isSinglePortEnabled()) {
				if (getSocket() != null)
					getSocket().close();
			} else {
				if (this.m_sock_nonssl != null) {
					this.m_sock_nonssl.close();
					this.m_sock = (this.m_sock_nonssl = null);
				}
				if (this.m_sock_ssl != null) {
					this.m_sock_ssl.close();
					this.m_sock = (this.m_sock_ssl = null);
				}
			}
		} catch (IOException localIOException) {
			Debug.out.println(localIOException);
		}
	}

	public String getipv6IP() {
		Enumeration localEnumeration1 = null;
		try {
			localEnumeration1 = NetworkInterface.getNetworkInterfaces();
		} catch (SocketException localSocketException) {
			Debug.out.println(localSocketException);
		}
		while (localEnumeration1.hasMoreElements()) {
			NetworkInterface localNetworkInterface = (NetworkInterface) localEnumeration1.nextElement();

			Debug.out.println("NetworkInterface name: " + localNetworkInterface.getDisplayName());

			Enumeration localEnumeration2 = localNetworkInterface.getInetAddresses();
			while (localEnumeration2.hasMoreElements()) {
				InetAddress localInetAddress1 = (InetAddress) localEnumeration2.nextElement();
				InetAddress localInetAddress2 = null;
				try {
					localInetAddress2 = InetAddress.getLocalHost();
				} catch (UnknownHostException localUnknownHostException) {
					Debug.out.println(localUnknownHostException);
				}
				String str1 = localInetAddress2.getHostAddress();

				String str2 = localInetAddress1.toString();
				String[] arrayOfString = str2.split("/");

				if (str1.compareTo(arrayOfString[1]) == 0) {
					Debug.out.println("CONTINUING LOOP");
				} else {
					Debug.out.println("" + isProperIPv6Address(arrayOfString[1]));
					if (isProperIPv6Address(arrayOfString[1]) == true) {
						Debug.out.println("VALID IPV6IP::" + arrayOfString[1]);
						return arrayOfString[1];
					}
				}
			}
		}

		return null;
	}

	public static boolean isProperIPv6Address(String paramString) {
		String[] arrayOfString1 = { "fc80", "fec0", "ff" };
		String[] arrayOfString2 = paramString.split(":");
		try {
			InetAddress localInetAddress = Inet6Address.getByName(paramString);
			if ((localInetAddress.isLinkLocalAddress()) || (localInetAddress.isLoopbackAddress())) {
				Debug.out.println("Returning false because LOOPBACK");
				return false;
			}
			for (int i = 0; i < arrayOfString1.length; i++) {
				if (arrayOfString2[0].compareToIgnoreCase(arrayOfString1[i]) == 0) {
					Debug.out.println("Returning false because RESERVED IP");

					return false;
				}
			}

			return true;
		} catch (Exception localException) {
			Debug.out.println(localException);
		}
		return false;
	}

	public abstract void onReadEvent();
}
